package com.mars.base.common.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;

public class EnvUtil {

	private static final String OS_NAME = System.getProperty("os.name").toLowerCase();

	/**
	 * 判断是否是Linux系统
	 * @return Boolean
	 */
	public static boolean isLinux(){
		if(OS_NAME.indexOf("linux")>=0){
			return true;
		}
		return false;
	}
	/**
	 * 判断是否是Windows系统
	 * @return Boolean
	 */
	public static boolean isWindows(){
		if(OS_NAME.indexOf("windows")>=0){
			return true;
		}
		return false;
	}
	/**
	 * 判断是否是Mac系统
	 * @return Boolean
	 */
	public static boolean isMac(){
		if(OS_NAME.indexOf("mac")>=0){
			return true;
		}
		return false;
	}
	/**
	 * 返回系统名称
	 * @return String
	 */
	public static String getOSName(){
		return OS_NAME;
	}
	

	/**
	 * 返回当前系统变量的函数 结果放至 Properties
	 */
	public static Properties getEnv() {
		Properties prop = new Properties();
		try {
			Process p = null;
			if (isLinux()) {
				p = Runtime.getRuntime().exec("sh -c set");
			} else {
				// windows
				p = Runtime.getRuntime().exec("cmd /c set");
			}
			BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String line;
			while ((line = br.readLine()) != null) {
				int i = line.indexOf("=");
				if (i > -1) {
					String key = line.substring(0, i);
					String value = line.substring(i + 1);
					prop.setProperty(key, value);
				}
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return prop;
	}
	
}
